home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.basic;
-
- import com.sun.java.swing.CellRendererPane;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.JTable;
- import com.sun.java.swing.LookAndFeel;
- import com.sun.java.swing.SwingUtilities;
- import com.sun.java.swing.plaf.ComponentUI;
- import com.sun.java.swing.plaf.TableHeaderUI;
- import com.sun.java.swing.table.JTableHeader;
- import com.sun.java.swing.table.TableCellRenderer;
- import com.sun.java.swing.table.TableColumn;
- import com.sun.java.swing.table.TableColumnModel;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Cursor;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.event.FocusEvent;
- import java.awt.event.FocusListener;
- import java.awt.event.InputEvent;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.io.Serializable;
- import java.util.Enumeration;
-
- public class BasicTableHeaderUI extends TableHeaderUI implements MouseListener, MouseMotionListener, FocusListener, Serializable {
- protected JTableHeader header;
- protected CellRendererPane rendererPane = new CellRendererPane();
- protected transient int hitColumnIndex;
- protected transient TableColumn hitColumn;
- protected transient boolean isResizing;
- protected transient int originalWidth;
- protected transient int widthDelta;
- protected transient int lastMouseX;
- protected transient int realDraggedDistance;
- protected transient boolean isReordering;
- protected transient boolean okToReorder;
- protected transient boolean fireAction;
- protected transient boolean hasPress = false;
- static final Cursor defaultCursor = Cursor.getPredefinedCursor(0);
- static final Cursor resizeCursor = Cursor.getPredefinedCursor(11);
-
- public static ComponentUI createUI(JComponent h) {
- return new BasicTableHeaderUI();
- }
-
- public void focusGained(FocusEvent e) {
- }
-
- public void focusLost(FocusEvent e) {
- }
-
- public Dimension getMaximumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getMinimumSize(JComponent c) {
- return this.getPreferredSize(c);
- }
-
- public Dimension getPreferredSize(JComponent c) {
- JTableHeader header = (JTableHeader)c;
- JTable table = header.getTable();
- Dimension tableSize = ((JComponent)table).getPreferredSize();
- Dimension size = new Dimension();
- size.width = tableSize.width;
- int column = 0;
-
- for(Enumeration enumeration = header.getColumnModel().getColumns(); enumeration.hasMoreElements(); ++column) {
- TableColumn aColumn = (TableColumn)enumeration.nextElement();
- TableCellRenderer renderer = aColumn.getHeaderRenderer();
- Component comp = renderer.getTableCellRendererComponent(header.getTable(), aColumn.getHeaderValue(), false, false, -1, column);
- size.height = Math.max(size.height, comp.getPreferredSize().height);
- }
-
- return size;
- }
-
- private int getResizingColumn(Point p) {
- int column = 0;
- Rectangle resizeRect = new Rectangle(-3, 0, 6, this.header.getSize().height);
- int columnMargin = this.header.getColumnModel().getColumnMargin();
-
- for(Enumeration enumeration = this.header.getColumnModel().getColumns(); enumeration.hasMoreElements(); ++column) {
- TableColumn aColumn = (TableColumn)enumeration.nextElement();
- resizeRect.x += aColumn.getWidth() + columnMargin;
- if (resizeRect.x > p.x) {
- break;
- }
-
- if (resizeRect.contains(p)) {
- return column;
- }
- }
-
- return -1;
- }
-
- public void installUI(JComponent c) {
- this.header = (JTableHeader)c;
- this.header.add(this.rendererPane);
- ((Component)c).addMouseListener(this);
- ((Component)c).addMouseMotionListener(this);
- ((Component)c).addFocusListener(this);
- LookAndFeel.installColorsAndFont(this.header, "TableHeader.background", "TableHeader.foreground", "TableHeader.font");
- }
-
- public void mouseClicked(MouseEvent e) {
- }
-
- public void mouseDragged(MouseEvent e) {
- this.fireAction = false;
- int mouseX = e.getX();
- if (mouseX != this.lastMouseX) {
- if (this.isResizing) {
- this.widthDelta += mouseX - this.lastMouseX;
- int newWidth = this.originalWidth + this.widthDelta;
- this.hitColumn.setWidth(newWidth);
- this.header.revalidate();
- this.header.repaint();
- if (this.header.getUpdateTableInRealTime()) {
- JTable table = this.header.getTable();
- ((JComponent)table).revalidate();
- ((Component)table).repaint();
- }
- } else if (this.okToReorder || this.isReordering) {
- this.isReordering = true;
- this.move(e);
- }
-
- this.lastMouseX = mouseX;
- }
- }
-
- public void mouseEntered(MouseEvent e) {
- }
-
- public void mouseExited(MouseEvent e) {
- }
-
- public void mouseMoved(MouseEvent e) {
- if (this.getResizingColumn(e.getPoint()) != -1) {
- if (this.header.getCursor() != resizeCursor) {
- this.header.setCursor(resizeCursor);
- }
- } else if (this.header.getCursor() != defaultCursor) {
- this.header.setCursor(defaultCursor);
- }
-
- }
-
- public void mousePressed(MouseEvent e) {
- if (!this.hasPress) {
- this.hasPress = true;
- TableColumnModel columnModel = this.header.getColumnModel();
- int index = 0;
- this.isReordering = false;
- this.isResizing = false;
- this.fireAction = true;
- this.hitColumnIndex = -1;
- this.hitColumn = null;
- Point p = e.getPoint();
- if ((index = columnModel.getColumnIndexAtX(p.x)) != -1) {
- Rectangle headerRect = this.header.getHeaderRect(index);
- int resizeIndex = this.getResizingColumn(p);
- if (this.header.getResizingAllowed() && resizeIndex != -1) {
- this.hitColumn = columnModel.getColumn(resizeIndex);
- if (this.hitColumn.getResizable()) {
- this.isResizing = true;
- this.fireAction = false;
- this.hitColumnIndex = resizeIndex;
- this.originalWidth = this.hitColumn.getWidth();
- this.widthDelta = 0;
- this.header.setResizingColumn(this.hitColumn);
- this.lastMouseX = p.x;
- this.hitColumn.disableResizedPosting();
- } else {
- this.hitColumn = null;
- }
- } else {
- boolean controlKeyDown = ((InputEvent)e).isControlDown();
- if (this.header.getReorderingAllowed() && headerRect.contains(p)) {
- this.okToReorder = columnModel.getColumnCount() > 1;
- if (controlKeyDown) {
- this.okToReorder = false;
- this.fireAction = false;
- }
- }
-
- JTable table = this.header.getTable();
- if (columnModel.getColumnSelectionAllowed() && headerRect.contains(p) && table != null) {
- if (controlKeyDown) {
- if (table.isColumnSelected(index)) {
- table.removeColumnSelectionInterval(index, index);
- } else {
- table.addColumnSelectionInterval(index, index);
- }
- } else {
- table.setColumnSelectionInterval(index, index);
- }
- }
-
- this.hitColumnIndex = index;
- this.header.setDraggedColumn(this.hitColumn);
- this.header.setDraggedDistance(0);
- this.lastMouseX = p.x;
- this.realDraggedDistance = 0;
- }
- }
-
- }
- }
-
- public void mouseReleased(MouseEvent e) {
- this.hasPress = false;
- if (this.isResizing) {
- this.isResizing = false;
- this.originalWidth = 0;
- this.header.setResizingColumn((TableColumn)null);
- int newWidth = this.hitColumn.getWidth();
- this.hitColumn.setWidth(this.originalWidth);
- this.hitColumn.enableResizedPosting();
- this.hitColumn.setWidth(newWidth);
- } else if (this.isReordering) {
- this.isReordering = false;
- this.originalWidth = this.widthDelta = 0;
- this.realDraggedDistance = 0;
- } else if (this.fireAction) {
- this.fireAction = false;
- }
-
- this.header.setDraggedColumn((TableColumn)null);
- this.header.setDraggedDistance(0);
- this.header.repaint();
- JTable table = this.header.getTable();
- if (table != null) {
- ((Component)table).repaint();
- }
-
- this.okToReorder = false;
- this.hitColumnIndex = -1;
- this.hitColumn = null;
- }
-
- private void move(MouseEvent e) {
- TableColumnModel columnModel = this.header.getColumnModel();
- int lastColumn = columnModel.getColumnCount() - 1;
- Rectangle redrawRect = this.header.getHeaderRect(this.hitColumnIndex);
- redrawRect.x += this.header.getDraggedDistance();
- int delta = e.getX() - this.lastMouseX;
- this.realDraggedDistance += delta;
- Rectangle redrawRect2 = this.header.getHeaderRect(this.hitColumnIndex);
- redrawRect2.x += this.realDraggedDistance;
- redrawRect = redrawRect.union(redrawRect2);
- if (this.realDraggedDistance < 0 && this.hitColumnIndex != 0) {
- int var10 = columnModel.getColumnMargin() + columnModel.getColumn(this.hitColumnIndex - 1).getWidth();
- if (-this.realDraggedDistance > var10 / 2) {
- columnModel.moveColumn(this.hitColumnIndex, this.hitColumnIndex - 1);
- this.realDraggedDistance += var10;
- --this.hitColumnIndex;
- }
- } else if (this.realDraggedDistance > 0 && this.hitColumnIndex != lastColumn) {
- int width = columnModel.getColumnMargin() + columnModel.getColumn(this.hitColumnIndex + 1).getWidth();
- if (this.realDraggedDistance > width / 2) {
- columnModel.moveColumn(this.hitColumnIndex, this.hitColumnIndex + 1);
- this.realDraggedDistance = -(width - this.realDraggedDistance);
- ++this.hitColumnIndex;
- }
- }
-
- int draggedDistance = this.realDraggedDistance;
- if (this.hitColumnIndex == 0) {
- if (draggedDistance < 0) {
- draggedDistance = 0;
- }
- } else if (this.hitColumnIndex == lastColumn && draggedDistance > 0) {
- draggedDistance = 0;
- }
-
- this.header.setDraggedColumn(columnModel.getColumn(this.hitColumnIndex));
- this.header.setDraggedDistance(draggedDistance);
- this.header.repaint(redrawRect.x, 0, redrawRect.width, redrawRect.height);
- if (this.header.getUpdateTableInRealTime()) {
- JTable table = this.header.getTable();
- if (table != null) {
- ((Component)table).repaint(redrawRect.x, 0, redrawRect.width, (table.getRowHeight() + table.getIntercellSpacing().height) * table.getRowCount());
- }
- }
-
- }
-
- public void paint(Graphics g, JComponent c) {
- JTableHeader header = (JTableHeader)c;
- Rectangle paintBounds = g.getClipBounds();
- Dimension size = ((Component)header).getSize();
- Component component = null;
- if (header.getColumnModel() != null) {
- Color bColor = ((Component)header).getParent().getBackground();
- if (bColor != null) {
- g.setColor(bColor);
- g.fillRect(paintBounds.x, paintBounds.y, paintBounds.width, paintBounds.height);
- }
-
- Rectangle cellRect = new Rectangle(0, 0, size.width, size.height);
- int column = 0;
- boolean drawn = false;
- Rectangle draggedCellRect = null;
- TableColumn draggedColumnObject = null;
- int columnMargin = header.getColumnModel().getColumnMargin();
-
- for(Enumeration enumeration = header.getColumnModel().getColumns(); enumeration.hasMoreElements(); ++column) {
- TableColumn aColumn = (TableColumn)enumeration.nextElement();
- cellRect.width = aColumn.getWidth() + columnMargin;
- if (cellRect.intersects(paintBounds)) {
- drawn = true;
- if (aColumn != header.getDraggedColumn()) {
- TableCellRenderer renderer = aColumn.getHeaderRenderer();
- component = renderer.getTableCellRendererComponent(header.getTable(), aColumn.getHeaderValue(), false, false, -1, column);
- this.rendererPane.add(component);
- this.rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y, cellRect.width, cellRect.height, true);
- } else {
- g.setColor(((Component)header).getParent().getBackground());
- g.fillRect(cellRect.x, cellRect.y, cellRect.width, cellRect.height);
- draggedCellRect = new Rectangle(cellRect);
- }
- } else if (drawn) {
- break;
- }
-
- cellRect.x += cellRect.width;
- }
-
- draggedColumnObject = header.getDraggedColumn();
- if (draggedColumnObject != null && draggedCellRect != null) {
- TableCellRenderer var20 = draggedColumnObject.getHeaderRenderer();
- component = var20.getTableCellRendererComponent(header.getTable(), draggedColumnObject.getHeaderValue(), false, false, -1, column);
- draggedCellRect.x += header.getDraggedDistance();
- SwingUtilities.paintComponent(g, component, header, draggedCellRect);
- }
-
- }
- }
-
- public void uninstallUI(JComponent c) {
- this.header.remove(this.rendererPane);
- this.header = null;
- ((Component)c).removeMouseListener(this);
- ((Component)c).removeMouseMotionListener(this);
- ((Component)c).removeFocusListener(this);
- }
- }
-